package com.ticket.system.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ticket.common.constant.HttpStatus;
import com.ticket.common.constant.PerformConstants;
import com.ticket.common.constant.UserConstants;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.utils.DateUtils;
import com.ticket.common.utils.Id2Utils;
import com.ticket.common.utils.NumberReplaceUtils;
import com.ticket.system.domain.YanchuUserInfo;
import com.ticket.system.domain.vo.UserInfoVo;
import com.ticket.system.mapper.YanchuUserInfoMapper;
import com.ticket.system.service.ITYanchuPointsRuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ticket.system.mapper.YanchuMoviegoerMapper;
import com.ticket.system.domain.YanchuMoviegoer;
import com.ticket.system.service.IYanchuMoviegoerService;
import org.springframework.util.CollectionUtils;

/**
 * 常用观演人信息关联关系Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-20
 */
@Service
public class YanchuMoviegoerServiceImpl implements IYanchuMoviegoerService {
    private Logger logger = LoggerFactory.getLogger(YanchuMoviegoerServiceImpl.class);
    @Autowired
    private YanchuMoviegoerMapper yanchuMoviegoerMapper;
    @Autowired
    private YanchuUserInfoMapper yanchuUserInfoMapper;

    @Autowired
    private ITYanchuPointsRuleService yanchuPointsRuleService;

    /**
     * 查询常用观演人信息关联关系
     *
     * @param id 常用观演人信息关联关系主键
     * @return 常用观演人信息关联关系
     */
    @Override
    public YanchuMoviegoer selectYanchuMoviegoerById(Long id) {
        return yanchuMoviegoerMapper.selectYanchuMoviegoerById(id);
    }

    /**
     * 查询常用观演人信息关联关系列表
     *
     * @param yanchuMoviegoer 常用观演人信息关联关系
     * @return 常用观演人信息关联关系
     */
    @Override
    public List<YanchuMoviegoer> selectYanchuMoviegoerList(YanchuMoviegoer yanchuMoviegoer) {
        return yanchuMoviegoerMapper.selectYanchuMoviegoerList(yanchuMoviegoer);
    }

    /**
     * 新增常用观演人信息关联关系
     *
     * @param yanchuMoviegoer 常用观演人信息关联关系
     * @return 结果
     */
    @Override
    public int insertYanchuMoviegoer(YanchuMoviegoer yanchuMoviegoer) {
        yanchuMoviegoer.setCreateTime(DateUtils.getNowDate());
        return yanchuMoviegoerMapper.insertYanchuMoviegoer(yanchuMoviegoer);
    }

    /**
     * 修改常用观演人信息关联关系
     *
     * @param yanchuMoviegoer 常用观演人信息关联关系
     * @return 结果
     */
    @Override
    public int updateYanchuMoviegoer(YanchuMoviegoer yanchuMoviegoer) {
        return yanchuMoviegoerMapper.updateYanchuMoviegoer(yanchuMoviegoer);
    }

    /**
     * 批量删除常用观演人信息关联关系
     *
     * @param ids 需要删除的常用观演人信息关联关系主键
     * @return 结果
     */
    @Override
    public int deleteYanchuMoviegoerByIds(Long[] ids) {
        return yanchuMoviegoerMapper.deleteYanchuMoviegoerByIds(ids);
    }

    /**
     * 删除常用观演人信息关联关系信息
     *
     * @param id 常用观演人信息关联关系主键
     * @return 结果
     */
    @Override
    public int deleteYanchuMoviegoerById(Long id) {
        return yanchuMoviegoerMapper.deleteYanchuMoviegoerById(id);
    }

    /**
     * 根据当前登录用户id查询关联的常用观演人信息
     *
     * @param curLoginUserId 当前登录用户id
     * @return
     */
    @Override
    public List<UserInfoVo> selectMoviegoerByUserinfoId(Long curLoginUserId) {
        List<UserInfoVo> userInfoVoList = yanchuMoviegoerMapper.selectMoviegoerByUserinfoId(curLoginUserId);
        if (!CollectionUtils.isEmpty(userInfoVoList)) {
            //根据证件类型判断处理替换*
            for (UserInfoVo userInfoVo : userInfoVoList) {
                if (userInfoVo.getIdType() == UserConstants.ID_TYPE_IDENTITY_CARD) {
                    //证件类型是身份证；
                    userInfoVo.setEncryptIdNumber(NumberReplaceUtils.idCardAddStar(userInfoVo.getIdNumber()));
                } else if (userInfoVo.getIdType() == UserConstants.ID_TYPE_PASSPORT) {
                    //证件类型是护照
                    userInfoVo.setEncryptIdNumber(NumberReplaceUtils.passportAddStar(userInfoVo.getIdNumber()));
                }
            }
        }
        return userInfoVoList;
    }

    /**
     * 添加常用观演人信息
     *
     * @param yanchuUserInfo 观演人信息
     * @param curLoginUserId 当前用户id
     * @return
     */
    @Override
    public AjaxResult addMoviegoer(YanchuUserInfo yanchuUserInfo, Long curLoginUserId) {
        //根据证件类型、编号查询校验数据是否已存在；
        //1、若数据已存在再判断观影人姓名是否一致；名字一致则获取相关id添加当前用户与观演人关联关系；若数据不一致返回错误提示给客户端；
        //2、若数据不存在先添加观演人信息，再新增当前用户与观演人关联关系数据；
        AjaxResult checkUserAjaxResult = null;
        Long userInfoId = null;
        YanchuUserInfo userInfo = new YanchuUserInfo();
        userInfo.setIdType(yanchuUserInfo.getIdType());
        userInfo.setIdNumber(yanchuUserInfo.getIdNumber());
        if(yanchuUserInfo.getIdType().equals(PerformConstants.ID_TYPE_3)){//港澳台
            userInfo.setUserName(yanchuUserInfo.getUserName());
        }
        List<YanchuUserInfo> userInfoList = yanchuUserInfoMapper.selectYanchuUserInfoList(userInfo);
        if (!CollectionUtils.isEmpty(userInfoList)) {
            if (!userInfoList.get(0).getUserName().equals(yanchuUserInfo.getUserName())) {
                //************实名认证start************
                checkUserAjaxResult = this.certification(yanchuUserInfo.getUserName(), yanchuUserInfo.getIdNumber());
                if (!checkUserAjaxResult.get(AjaxResult.CODE_TAG).toString().equals(HttpStatus.SUCCESS+"")) {
                    return AjaxResult.error(checkUserAjaxResult.get("msg").toString());
                }
                //************实名认证end************
                //更新观演人姓名
                userInfoList.get(0).setUserName(yanchuUserInfo.getUserName());
                userInfoList.get(0).setUpdateTime(new Date());
                yanchuUserInfoMapper.updateYanchuUserInfo(userInfoList.get(0));
            }
            userInfoId = userInfoList.get(0).getId();
        } else {
            if(yanchuUserInfo.getIdType().equals(PerformConstants.ID_TYPE_1)){
                //************实名认证start************
                checkUserAjaxResult = this.certification(yanchuUserInfo.getUserName(), yanchuUserInfo.getIdNumber());
                if (!checkUserAjaxResult.get(AjaxResult.CODE_TAG).toString().equals(HttpStatus.SUCCESS+"")) {
                    return AjaxResult.error(checkUserAjaxResult.get("msg").toString());
                }
            }
            //************实名认证end************
            yanchuUserInfoMapper.insertYanchuUserInfo(yanchuUserInfo);
            userInfoId = yanchuUserInfo.getId();
        }
        YanchuMoviegoer yanchuMoviegoer = new YanchuMoviegoer();
        yanchuMoviegoer.setUserinfoId(curLoginUserId);
        yanchuMoviegoer.setMoviegoerUserinfoId(userInfoId);
        yanchuMoviegoer.setCreateTime(new Date());
        yanchuMoviegoerMapper.insertYanchuMoviegoer(yanchuMoviegoer);
        yanchuPointsRuleService.addPoints(8,null,curLoginUserId);
        return AjaxResult.success("添加成功");
    }

    /**
     * 常用观演人实名认证
     *
     * @param userName 姓名
     * @param idNumber 身份证号码
     * @return
     */
    @Override
    public AjaxResult certification(String userName, String idNumber) {
        try {
            JSONObject jsonObject = Id2Utils.postData(userName, idNumber);
            logger.info("certification:{}",jsonObject);
            if (null == jsonObject) {
                return AjaxResult.error("实名认证未返回信息");
            } else if (null != jsonObject && jsonObject.get("respCode").equals(Id2Utils.RESPCODE_SUCCESS)) {
                return AjaxResult.success(jsonObject);
            } else {
                return AjaxResult.error(jsonObject.getString("respMessage"));
            }
        } catch (IOException e) {
            logger.error("certification IOException:{}", e);
            return AjaxResult.error("实名认证操作异常");
        }
    }
}
